package com.hanhai.zrb.mobile.interceptor;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.hanhai.common.util.CommonUtil;
import com.hanhai.zrb.api.biz.service.UserBlacklistService;
import com.hanhai.zrb.mobile.util.FrontLoginUtil;
import com.hanhai.zrb.model.common.BaseUser;

/**
 * 会员管理-黑名单拦截，禁止登录和访问
 *
 */
public class UserBlacklistInterceptor extends HandlerInterceptorAdapter {

	@Resource
	private UserBlacklistService userBlacklistService;

	public static final Logger logger = LoggerFactory
			.getLogger(UserBlacklistInterceptor.class);

	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {	
		BaseUser user = FrontLoginUtil.getCurrentUser();
		if(user == null){
			return true;
		}
	
		List<Long> userIdListInBlacklist = userBlacklistService.listAllUserIdListNotExpired();
		boolean isBlack = userIdListInBlacklist.contains(user.getUid());
		if (isBlack) {
			logger.error("Find a request in blacklist,uid="+user.getUid());
			CommonUtil.jsonMobileBlacklist(response);
			return false;
		}
		return true;
	}

	@Override
	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		super.postHandle(request, response, handler, modelAndView);
	}

	@Override
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		super.afterCompletion(request, response, handler, ex);
	}

}
